import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

// 登录、注册
import Auth from "@/pages/auth/Auth";
import Login from "@/pages/auth/login/Login";
import Register from "@/pages/auth/register/Register";

// 主页
import Home from "@/pages/home/Home";
import Panorama from "@/pages/home/panorama/Panorama";
import Good from "@/pages/home/panorama/views/Good";
import Room from "@/pages/home/panorama/views/Room";
import GoodDetail from "@/pages/home/GoodDetail";
import RoomDetail from "@/pages/home/RoomDetail";

// 后台
import Back from "@/pages/back/Back";
// 后台--个人信息
import Person from "@/pages/back/person/Person";
// 后台--我的发布
import Published from "@/pages/back/publish/Published";
import MyGood from "@/pages/back/publish/views/MyGood";
import MyRoom from "@/pages/back/publish/views/MyRoom";
// 后台--交易记录
import Record from "@/pages/back/record/Record";
import BuyGood from "@/pages/back/record/buyer/BuyGood";
import BuyRoom from "@/pages/back/record/buyer/BuyRoom";
import SoldGood from "@/pages/back/record/seller/SoldGood";
import SoldRoom from "@/pages/back/record/seller/SoldRoom";
// 后台--审核
import Examine from "@/pages/back/examine/Examine";
import ExamUser from "@/pages/back/examine/views/ExamUser";
import ExamGood from "@/pages/back/examine/views/ExamGood";
import ExamRoom from "@/pages/back/examine/views/ExamRoom";
// 后台--角色管理
import Role from "@/pages/back/role/Role";

const router = new VueRouter({
    routes: [{
            path: "/",
            redirect: (to) => {
                return {
                    name: "good"
                };
            },
        },
        {
            name: "auth",
            path: "/auth",
            component: Auth,
            redirect: (to) => {
                return {
                    name: "login"
                };
            },
            meta: {
                requireAuth: true,
                rules: ["superadmin", "admin", "user"],
            },
            children: [{
                    name: "register",
                    path: "register",
                    component: Register,
                    meta: {
                        requireAuth: false,
                        rules: ["superadmin", "admin", "user"],
                    },
                },
                {
                    name: "login",
                    path: "login",
                    component: Login,
                    meta: {
                        requireAuth: false,
                        rules: ["superadmin", "admin", "user"],
                    },
                },
            ],
        },
        {
            name: "home",
            path: "/home",
            component: Home,
            redirect: (to) => {
                return {
                    name: "good"
                };
            },
            meta: {
                requireAuth: true,
                rules: ["superadmin", "admin", "user"],
            },
            children: [{
                    name: "panorama",
                    path: "panorama",
                    component: Panorama,
                    redirect: (to) => {
                        return {
                            name: "login"
                        };
                    },
                    meta: {
                        requireAuth: true,
                        rules: ["superadmin", "admin", "user"],
                    },
                    children: [{
                            name: "good",
                            path: "good",
                            component: Good,
                            meta: {
                                requireAuth: true,
                                rules: ["superadmin", "admin", "user"],
                            },
                        },
                        {
                            name: "room",
                            path: "room",
                            component: Room,
                            meta: {
                                requireAuth: true,
                                rules: ["superadmin", "admin", "user"],
                            },
                        },
                    ],
                },
                {
                    name: "goodDetail",
                    path: "goodDetail",
                    component: GoodDetail,
                    meta: {
                        requireAuth: true,
                        rules: ["superadmin", "admin", "user"],
                    },
                },
                {
                    name: "roomDetail",
                    path: "roomDetail",
                    component: RoomDetail,
                    meta: {
                        requireAuth: true,
                        rules: ["superadmin", "admin", "user"],
                    },
                },
            ],
        },
        {
            name: "back",
            path: "/back",
            redirect: (to) => {
                return {
                    name: "person"
                };
            },
            component: Back,
            meta: {
                requireAuth: true,
                rules: ["superadmin", "admin", "user"],
            },
            children: [{
                    name: "person",
                    path: "person",
                    component: Person,
                    meta: {
                        requireAuth: true,
                        rules: ["superadmin", "admin", "user"],
                    },
                },
                {
                    name: "published",
                    path: "published",
                    component: Published,
                    redirect: (to) => {
                        return {
                            name: "mygood"
                        };
                    },
                    meta: {
                        requireAuth: true,
                        rules: ["superadmin", "admin", "user"],
                    },
                    children: [{
                            name: "mygood",
                            path: "mygood",
                            component: MyGood,
                            meta: {
                                requireAuth: true,
                                rules: ["superadmin", "admin", "user"],
                            },
                        },
                        {
                            name: "myroom",
                            path: "myroom",
                            component: MyRoom,
                            meta: {
                                requireAuth: true,
                                rules: ["superadmin", "admin", "user"],
                            },
                        },
                    ],
                },
                {
                    name: "record",
                    path: "record",
                    component: Record,
                    redirect: (to) => {
                        return {
                            name: "buygood"
                        };
                    },
                    meta: {
                        requireAuth: true,
                        rules: ["superadmin", "admin", "user"],
                    },
                    children: [{
                            name: "buygood",
                            path: "buygood",
                            component: BuyGood,
                            meta: {
                                requireAuth: true,
                                rules: ["superadmin", "admin", "user"],
                            },
                        },
                        {
                            name: "buyroom",
                            path: "buyroom",
                            component: BuyRoom,
                            meta: {
                                requireAuth: true,
                                rules: ["superadmin", "admin", "user"],
                            },
                        },
                        {
                            name: "soldgood",
                            path: "soldgood",
                            component: SoldGood,
                            meta: {
                                requireAuth: true,
                                rules: ["superadmin", "admin", "user"],
                            },
                        },
                        {
                            name: "soldroom",
                            path: "soldroom",
                            component: SoldRoom,
                            meta: {
                                requireAuth: true,
                                rules: ["superadmin", "admin", "user"],
                            },
                        },
                    ],
                },
                {
                    name: "examine",
                    path: "examine",
                    redirect: (to) => {
                        return {
                            name: "examuser"
                        };
                    },
                    meta: {
                        requireAuth: true,
                        rules: ["superadmin"],
                    },
                    component: Examine,
                    children: [{
                            name: "examuser",
                            path: "examuser",
                            component: ExamUser,
                            meta: {
                                requireAuth: true,
                                rules: ["superadmin", "admin"],
                            },
                        },
                        {
                            name: "examgood",
                            path: "examgood",
                            component: ExamGood,
                            meta: {
                                requireAuth: true,
                                rules: ["superadmin", "admin"],
                            },
                        },
                        {
                            name: "examroom",
                            path: "examroom",
                            component: ExamRoom,
                            meta: {
                                requireAuth: true,
                                rules: ["superadmin", "admin"],
                            },
                        },
                    ],
                },
                {
                    name: "role",
                    path: "role",
                    component: Role,
                    meta: {
                        requireAuth: true,
                        rules: ["superadmin"],
                    }
                }
            ],
        },
    ],
});

// 前置守卫路由
router.beforeEach((to, from, next) => {
    let isLogin = sessionStorage.getItem("authUser") == undefined ? false : true;
    if (to.matched.length == 0) {
        console.log(to);
        next({
            name: "good"
        });
    }
    // 登录页、注册页
    if (to.name == "login" || to.name == "register") {
        if (isLogin) {
            next({
                name: "good"
            });
        } else {
            next();
        }
    } else {
        // 除了auth外的其他页面
        // 是否需要登录
        if (to.meta.requireAuth) {
            if (isLogin) {
                next();
            } else {
                next({
                    name: "login"
                });
            }
        } else {
            next();
        }
    }
});

export default router;